﻿using System;
using System.Text.RegularExpressions;
using System.Windows.Forms;
using IceCS.GoodsSale.BLL;
using IceCS.GoodsSale.Models.UserModels;

namespace IceCS.GoodsSale.Winform
{
    public partial class FrmPswProInfo : Form
    {
        public FrmPswProInfo()
        {
            InitializeComponent();
        }

        string userName = UserManager.userInfo[0];
        UserPswProInfoManager userPswProInfoManager = new UserPswProInfoManager();

        private void FrmPswProInfo_Load(object sender, EventArgs e)
        {
            try
            {
                if (userPswProInfoManager.GetUserPswProInfoIsExist(userName))
                {
                    gbPswProtection.Text = "您已经设置了密码保护信息，您可以重新设置或删除这些信息";
                    UserPswProInfo userPswProInfo = new UserPswProInfo();
                    userPswProInfo = userPswProInfoManager.GetUserPswProInfo(userName);
                    txtName.Text = userPswProInfo.Name;
                    txtHometown.Text = userPswProInfo.Hometown;
                    txtPhoneNumber.Text = userPswProInfo.PhoneNumber;
                    txtEMail.Text = userPswProInfo.EMail;
                    txtName.SelectAll();
                    btnDeletePswProInfo.Enabled = true;
                }
                else
                {
                    gbPswProtection.Text = "您还没有设置密码保护信息，请设置";
                    btnDeletePswProInfo.Enabled = false;
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private void btnAddPswProInfo_Click(object sender, EventArgs e)
        {
            try
            {
                string name = txtName.Text.Trim();
                string hometown = txtHometown.Text.Trim();
                string phoneNumber = txtPhoneNumber.Text.Trim();
                string eMail = txtEMail.Text.Trim();
                if (name == "")
                {
                    MessageBox.Show("姓名不能为空，请输入", "提示");
                    txtName.Focus();
                    return;
                }
                if (hometown == "")
                {
                    MessageBox.Show("出生地不能为空，请输入", "提示");
                    txtHometown.Focus();
                    return;
                }
                string regexPhoneNumber = @"^\d{11}$";
                if (!Regex.IsMatch(phoneNumber, regexPhoneNumber))
                {
                    MessageBox.Show("手机号码格式不正确，请重新输入", "提示");
                    txtPhoneNumber.Text = "";
                    txtPhoneNumber.Focus();
                    return;
                }
                string regexEmail = @"\w{1,}@\w{1,}\.";
                if (!Regex.IsMatch(eMail, regexEmail))
                {
                    MessageBox.Show("E-Mail格式不正确，请重新输入", "提示");
                    txtEMail.Text = "";
                    txtEMail.Focus();
                    return;
                }
                UserPswProInfo userPswProInfo = new UserPswProInfo();
                userPswProInfo.UserName = userName;
                userPswProInfo.Name = name;
                userPswProInfo.Hometown = hometown;
                userPswProInfo.PhoneNumber = phoneNumber;
                userPswProInfo.EMail = eMail;
                if (userPswProInfoManager.GetUserPswProInfoIsExist(userName))
                {
                    if (userPswProInfoManager.ChangeUserPswProInfo(userPswProInfo))
                    {
                        MessageBox.Show("您的密码保护信息已经修改成功，请牢记", "提示");
                    }
                }
                else
                {
                    if (userPswProInfoManager.AddUserPswProInfo(userPswProInfo))
                    {
                        gbPswProtection.Text = "您已经设置了密码保护信息，您可以重新设置或删除这些信息";
                        btnDeletePswProInfo.Enabled = true;
                        MessageBox.Show("您的密码保护信息已经设置成功，请牢记", "提示");
                    }
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private void btnDeletePswProInfo_Click(object sender, EventArgs e)
        {
            try
            {
                if (MessageBox.Show("确定要删除您的密码保护信息吗?", "删除密码保护信息", MessageBoxButtons.YesNo, MessageBoxIcon.Question, MessageBoxDefaultButton.Button2) == DialogResult.Yes)
                {
                    if (userPswProInfoManager.RemoveUserPswProInfo(userName))
                    {
                        txtName.Text = "";
                        txtHometown.Text = "";
                        txtPhoneNumber.Text = "";
                        txtEMail.Text = "";
                        gbPswProtection.Text = "您还没有设置密码保护信息，请设置";
                        btnDeletePswProInfo.Enabled = false;
                        MessageBox.Show("您的密码保护信息已经删除成功，一旦忘了密码，将无法找回", "提示");
                        txtName.Focus();
                    }
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private void btnChangePassword_Click(object sender, EventArgs e)
        {
            try
            {
                PublicMethodsManager publicMethodsManager = new PublicMethodsManager();
                string md5OfPassword = publicMethodsManager.GetMd5OfPassword(txtOldPassword.Text.Trim());
                if (md5OfPassword != UserManager.userInfo[1])
                {
                    MessageBox.Show("旧密码不正确，请重新输入", "提示");
                    txtOldPassword.Text = "";
                    txtOldPassword.Focus();
                    return;
                }
                if (txtNewPassword1.Text.Trim().Length < 6 || txtNewPassword1.Text.Trim().Length > 32)
                {
                    MessageBox.Show("密码为6-32个字符，请重新输入", "提示");
                    txtNewPassword1.Text = "";
                    txtNewPassword1.Focus();
                    return;
                }
                if (txtNewPassword1.Text.Trim() != txtNewPassword2.Text.Trim())
                {
                    MessageBox.Show("两次输入的新密码不一致，请重新输入", "提示");
                    txtNewPassword1.Text = "";
                    txtNewPassword2.Text = "";
                    txtNewPassword1.Focus();
                    return;
                }
                string userPassword = txtNewPassword1.Text.Trim();
                UserManager userManager = new UserManager();
                if (userManager.ChangeUserPassword(userName, userPassword))
                {
                    MessageBox.Show("密码修改成功，请记住您的新密码", "提示");
                    txtOldPassword.Text = "";
                    txtNewPassword1.Text = "";
                    txtNewPassword2.Text = "";
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private void btnClose_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void txtNewPassword2_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                btnChangePassword_Click(sender, e);
            }
        }
    }
}
